﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Diagnostics.Contracts;

namespace BPMS.Core
{
    public class ConnectionInfo
    {
        public string ConnectionString { get; private set; }
        public string DbProviderName { get; private set; }

        private static readonly Dictionary<string, ConnectionStringSettings> connectionDictionary = new Dictionary<string, ConnectionStringSettings>();

        static ConnectionInfo()
        {
            ConnectionStringSettingsCollection collection = ConfigurationManager.ConnectionStrings;
            if (null != collection)
            {
                foreach (ConnectionStringSettings connItem in collection)
                {
                    connectionDictionary.Add(connItem.Name, connItem);
                }
            }
        }

        private ConnectionInfo(string connectionName)
        {
            ConnectionStringSettings connectionSection = null;
            if (connectionDictionary.TryGetValue(connectionName, out connectionSection))
            {
                ConnectionString = connectionSection.ConnectionString;
                DbProviderName = connectionSection.ProviderName;
            }
            else
            {
                ConnectionString = "Data Source=.;Integrated Security=False;User ID=sa;Password=abc123;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False";
                DbProviderName = "System.Data.SqlClient";
            }
        }

        public static ConnectionInfo DefaultConn = new ConnectionInfo("Default");
    }
}
